package arch.dao.impl.manager;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import arch.dao.manager.ArchLoggerDao;
import arch.domain.manager.ArchLogger;

@Repository("archLoggerDao")
public class ArchLoggerImpl implements ArchLoggerDao {
	@Resource private JdbcTemplate jdbcTemplate;
	
	public Integer query(String sql, Object... objects) {
		return this.jdbcTemplate.queryForInt(sql,objects);
	}

	@SuppressWarnings("unchecked")
	public List<ArchLogger> queryList(String sql, Object... objects) {
		return this.jdbcTemplate.query(sql, objects, new ArchLoggerMapper());
	}

	public ArchLogger find(Serializable primaryKey) {
		String sql = "select ID, OPERATOR_ID, OPERATOR_NAME, OPERATOR_DEPT_ID, OPERATOR_DEPT_NAME, LOGIN_IP, ARCH_ID, MODULE_NAME, "
			+"OPERATE_CONTENT, DETAIL, TYPE, SUB_TYPE, CLIENT_OPERATE_TIME,SERVER_CURRENT_TIME, SERVER_RUN_TIME, RESULT, DELETE "
			+" from AR_LOGGER where ID = ? ";
		try {
			return (ArchLogger)this.jdbcTemplate.queryForObject(sql, new Object[]{primaryKey},new ArchLoggerMapper());
		} catch (EmptyResultDataAccessException e) {
			return null;
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public void merge(ArchLogger entity) {
		// TODO Auto-generated method stub

	}

	public void persist(ArchLogger entity) {
		String sql = "insert into AR_LOGGER (ID, OPERATOR_ID, OPERATOR_NAME, OPERATOR_DEPT_ID, OPERATOR_DEPT_NAME, LOGIN_IP, ARCH_ID, MODULE_NAME, "
			+"OPERATE_CONTENT, DETAIL, TYPE, SUB_TYPE, CLIENT_OPERATE_TIME,SERVER_CURRENT_TIME, SERVER_RUN_TIME, RESULT, DELETE) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		this.jdbcTemplate.update(sql,new Object[]{
				entity.getId(),
				entity.getOperator()==null?null:entity.getOperator().getUserId(),
				entity.getOperator()==null?null:entity.getOperator().getUserName(),
				entity.getOperatorDept()==null?null:entity.getOperatorDept().getDeptId(),
				entity.getOperatorDept()==null?null:entity.getOperatorDept().getDeptName(),
				entity.getLoginIp(),
				entity.getArchId(),
				entity.getModuleName(),
				entity.getOperatContent(),
				entity.getDetail(),
				entity.getType()==null?null:entity.getType().name(),
				entity.getSubType(),
				entity.getClientOperateTime(),
				entity.getServerCurrentTime(),
				entity.getServerRunTime(),
				entity.getResult()?"1":"0",
				entity.getDelete()?"1":"0"		
		});
	}

	public void remove(Serializable primaryKey) {
		String sql = "update AR_LOGGER set DELETE = '1' where ID = ?  ";
		this.jdbcTemplate.update(sql,new Object[]{primaryKey});
	}

	public void persist(List<ArchLogger> loggers) {
		String sql = "insert into AR_LOGGER (ID, OPERATOR_ID, OPERATOR_NAME, OPERATOR_DEPT_ID, OPERATOR_DEPT_NAME, LOGIN_IP, ARCH_ID, MODULE_NAME, "
			+"OPERATE_CONTENT, DETAIL, TYPE, SUB_TYPE, CLIENT_OPERATE_TIME,SERVER_CURRENT_TIME, SERVER_RUN_TIME, RESULT, DELETE) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		this.jdbcTemplate.batchUpdate(sql, new ArchLoggerAddSetter(loggers));
		
	}

	public void remove(List<ArchLogger> loggers) {
		String sql = "update AR_LOGGER set DELETE = '1' where ID = ? ";
		this.jdbcTemplate.batchUpdate(sql, new ArchLoggerDeleteSetter(loggers));
	}

}
